/*
Filename: UCAddtickets.java
Precondition: manager login the system successful
Post-condition: manager add tickets successful
1. Manager selects to login.
2. Manager enters name and password.
3. Manager clicks ‘login’ button.
4. System checks manager name and password are valid
5. System checks with database that manager is unique.
6. Manager login the database
7. Manager can add tickets information into the system.
8. System displays message “Add tickets Successful.
9. System updates the main UI.
2a. Extensions: System detects invalid manager login:
 System display “Add tickets fail “message.
 */

package ucm;

/**
 *
 * @author Mark
 */
import model.InputModel;
import model.DatabaseModel;
import view.AddTicket;

import javax.swing.*;
import java.sql.Connection;
import java.sql.Statement;
import java.sql.ResultSet;

public class UCAddtickets implements UCController {

    private InputModel user;

    public UCAddtickets() {
        user = new InputModel("", "", "", "", "", "", "", "", "");
    }

    public UCAddtickets(InputModel user) {
        this.user = user;
    }

    public UCAddtickets(AddTicket AddTicket) {
        this.user = AddTicket.getUserModel();
    }
    public boolean run() {
        boolean addTicketSuccessful = false;
        if (validDetails()) {

                if (addone()) {
                    JOptionPane.showMessageDialog(new JFrame(),
                            "Add ticket success!",
                            "Success",
                            JOptionPane.INFORMATION_MESSAGE);
                      new ucm.UCDisplayManagement();
                    addTicketSuccessful = true;
                } else {
                    JOptionPane.showMessageDialog(new JFrame(),
                            "Registration failed for unknown reason",
                            "Error Message",
                            JOptionPane.ERROR_MESSAGE);
                }

        } else {

            JOptionPane.showMessageDialog(new JFrame(),
                    "Please input data in to *  !",
                    "Error Message",
                    JOptionPane.ERROR_MESSAGE);
            new view.AddTicket().setVisible(true);
        }
        return addTicketSuccessful;
    }

    public void run(InputModel user) {
        this.user = user;
        run();
    }

    private boolean validDetails() {
        return (user.getflight() != null) &&
                  (user.getfrom() != null) &&
                  (user.getfromtime() != null) &&
                  (user.getto() != null) &&
                  (user.gettotime() != null) &&
                  (user.getseat() != null) &&
                  (user.getprice() != null);
    }

    private boolean addone(){
        boolean add = false;
        String flight = user.getflight();
        String from = user.getfrom();
        String fromtime = user.getfromtime();
        String to = user.getto();
        String totime = user.gettotime();
        String seat = user.getseat();
        String price = user.getprice();

        DatabaseModel dbModel = DatabaseModel.getInstance();

        Connection con = dbModel.getConnection();
        try {
            Statement stmt = con.createStatement(ResultSet.CONCUR_UPDATABLE,
                    ResultSet.CONCUR_UPDATABLE);

            stmt.executeUpdate("INSERT INTO ticketsaler.tickets (`TFLIGHT`, `TFROM`, `TFROMTIME`, `TARRIVE`, `TARRIVETIME`, `TPRICE`, `TSEATS`) " +
                    "VALUES ('" + flight + "', '" + from + "', '" + fromtime + "', '" + to + "', '" + totime + "', " + price + ", " + seat + ")");
            add = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return add;
    }
}